package io.renren.modules.clothingtype.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.JacksonUtils;
import io.renren.common.utils.SubjectNestedVO;
import io.renren.modules.clothingpicture.dto.ClothingPictureDTO;
import io.renren.modules.clothingpicture.service.ClothingPictureService;
import io.renren.modules.clothingpicture.vo.ClothingPictureVO;
import io.renren.modules.clothingtype.dao.ClothingTypeDao;
import io.renren.modules.clothingtype.dto.ClothingTypeDTO;
import io.renren.modules.clothingtype.entity.ClothingTypeEntity;
import io.renren.modules.clothingtype.service.ClothingTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 服装类型表
 *
 * @author lzy lzy
 * @since 1.0.0 2021-08-30
 */
@Slf4j
@Service
public class ClothingTypeServiceImpl extends ServiceImpl<ClothingTypeDao, ClothingTypeEntity> implements ClothingTypeService {

    @Autowired
    private ClothingPictureService clothingPictureService;

    @Override
    public void save(ClothingTypeDTO clothingTypeDTO) {
        ClothingTypeEntity clothingTypeEntity = JacksonUtils.obj2pojo(clothingTypeDTO, ClothingTypeEntity.class);
        this.save(clothingTypeEntity);
    }

    @Override
    public void update(ClothingTypeDTO clothingTypeDTO) {
        ClothingTypeEntity clothingTypeEntity = JacksonUtils.obj2pojo(clothingTypeDTO, ClothingTypeEntity.class);
        this.updateById(clothingTypeEntity);
    }

    @Override
    public List<SubjectNestedVO> typePage(Map<String, Object> params) {
        log.info("服装类型列表展示");
        List<SubjectNestedVO> list = new ArrayList<SubjectNestedVO>();
        //获取所有的衣服信息集合
        List<ClothingTypeEntity> clothingTypeEntityList = this.baseMapper.selectList(null);
        //过滤pid=0的集合
        List<ClothingTypeEntity> collectPid = clothingTypeEntityList.stream().filter(e -> e.getPid() == 0L).collect(Collectors.toList());
        //过滤pid != 0 的集合
        List<ClothingTypeEntity> collectCid = clothingTypeEntityList.stream().filter(e -> e.getPid() != 0L).collect(Collectors.toList());

        int collectPidsize = collectPid.size();
        for (int i = 0; i < collectPidsize; i ++){
            SubjectNestedVO subjectNestedVO = new SubjectNestedVO();

            ClothingTypeEntity eduSubject = collectPid.get(i);
            //父级id
            Long pid = eduSubject.getId();
            //父级名称
            String title = eduSubject.getDictName();
            subjectNestedVO.setId(pid);
            subjectNestedVO.setDictName(title);
            List<ClothingTypeEntity> collect = collectCid.stream().filter(e -> e.getPid().equals(pid)).collect(Collectors.toList());
            List<SubjectNestedVO> children = new ArrayList<>();
            for (ClothingTypeEntity subject : collect) {
                SubjectNestedVO subjectNested = new SubjectNestedVO();
                Long cid = subject.getId();
                subjectNested.setId(cid);
                ClothingPictureVO clothingPictureVO = clothingPictureService.queryPictureByTypeId(cid);
                subjectNested.setPictureUrl(clothingPictureVO == null ? null : clothingPictureVO.getPicture());
                subjectNested.setDictName(subject.getDictName());
                children.add(subjectNested);
            }
            subjectNestedVO.setSubjectNestedList(children);
            list.add(subjectNestedVO);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addClothingType(ClothingTypeDTO dto) {
        log.info("服装类型添加");
        ClothingTypeEntity clothingTypeEntity = new ClothingTypeEntity();
        // 判断是否是增加父类
        Long pid = dto.getPid();
        String dictName = dto.getDictName();
        String pictureUrl = dto.getPictureUrl();
        clothingTypeEntity.setPid(pid);
        clothingTypeEntity.setDictName(dictName);
        this.baseMapper.insert(clothingTypeEntity);
        if (StringUtils.isNotBlank(pictureUrl)) {
            ClothingPictureDTO clothingPictureDTO = new ClothingPictureDTO();
            clothingPictureDTO.setClothingTypeId(clothingTypeEntity.getId());
            clothingPictureDTO.setPicture(pictureUrl);
            clothingPictureService.save(clothingPictureDTO);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeClothingType(ClothingTypeDTO dto) {
        log.info("服装类型修改");
        Long id = dto.getId();
        String dictName = dto.getDictName();
        String pictureUrl = dto.getPictureUrl();
        ClothingTypeEntity clothingTypeEntity = new ClothingTypeEntity();
        clothingTypeEntity.setDictName(dictName);
        UpdateWrapper<ClothingTypeEntity> wrapper = new UpdateWrapper<ClothingTypeEntity>();
        wrapper.eq("id", id);
        this.baseMapper.update(clothingTypeEntity, wrapper);
        if (StringUtils.isNotBlank(pictureUrl)) {
            clothingPictureService.updatePicture(id, pictureUrl);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteClothingType(List<Long> ids) {
        log.info("服装类型删除");
        for (Long id : ids) {
            ClothingTypeEntity clothingTypeEntity = this.baseMapper.selectById(id);
            if (clothingTypeEntity.getPid() == 0L){
                this.baseMapper.deleteById(id);
                QueryWrapper<ClothingTypeEntity> wrapper = new QueryWrapper<ClothingTypeEntity>();
                wrapper.eq("pid",id);
                List<ClothingTypeEntity> clothingTypeEntityList = this.baseMapper.selectList(wrapper);
                for (ClothingTypeEntity typeEntity : clothingTypeEntityList) {
                    this.baseMapper.deleteById(typeEntity.getId());
                    clothingPictureService.deleteTypeId(typeEntity.getId());
                }
            }else {
                this.baseMapper.deleteById(id);
                clothingPictureService.deleteTypeId(id);
            }
        }
    }
}
